﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class BufferPool
{
    //容器
    private static Dictionary<string, List<GameObject>> Pool = new Dictionary<string, List<GameObject>>();

    /// <summary>
    /// 创建游戏物体
    /// </summary>
    /// <param name="path"> 在resources下的物体路径</param>
    /// <returns></returns>
    public static GameObject Instantiate(string path)
    {
        if (!Pool.ContainsKey(path))
        {
            Pool.Add(path, new List<GameObject>());
        }
        //有就找
        List<GameObject> list = Pool[path];
        for (int i = 0; i < list.Count; i++)
        {
            if (!list[i].activeSelf)
            {
                list[i].SetActive(true);
                return list[i];
            }
        }
        //没有就创建
        GameObject perfab = Resources.Load<GameObject>(path);
        GameObject go = Object.Instantiate(perfab, Random.insideUnitSphere, Quaternion.identity);
        Pool[path].Add(go);
        return go;
    }
}